#include "list.h"
#include <string.h>
struct my_data {
    int age;
    char name[20];
    struct list_head entry;  
};

int main(void)
{
    struct list_head my_list;  //  定义头节点
    INIT_LIST_HEAD(&my_list);  //  初始化头节点
    struct my_data stu1 = {18, "Tom"};
    struct my_data stu2 = {19, "Jerry"};
    struct my_data stu3 = {20, "Jack"};
    struct my_data stu4 = {30, "Zhangsan"};
    struct my_data stu5 = {100, "Lisi"};
    struct my_data stu6 = {40, "Songxiaobao"};
    list_add(&stu1.entry, &my_list);  // 插入
    list_add(&stu2.entry, &my_list); 
    list_add_tail(&stu3.entry, &my_list); // 尾部插入
    list_add_tail(&stu4.entry, &my_list);  // 尾部插入
    list_add(&stu5.entry, &my_list); 
    list_add(&stu6.entry, &my_list); 
    
    
    printf("-- 遍历 --\n");
    struct my_data *pos, *next; 
    list_for_each_entry(pos, &my_list, entry) {
        printf("%s\n", pos->name);
       // if(strcmp(pos->name, "Tom") == 0)
         //   list_del(&pos->entry);
    }
    
    printf("-- 删除后 --\n");
    list_del(&stu2.entry);
    list_for_each_entry(pos, &my_list, entry) {
        printf("%s\n", pos->name);
    }    
     
    printf("-- 边删除边遍历 --\n");
    
    list_for_each_entry_safe(pos, next, &my_list, entry) {
        printf("%s\n", pos->name);
        list_del(&pos->entry); // 删除
    }    
    
    if (list_empty(&my_list)) {
        printf("链表为空\n");
    }
    
    
    list_add(&stu1.entry, &my_list);  // 插入
    list_add(&stu2.entry, &my_list); 
    printf("container_of 测试\n");
    struct list_head *cur;
    list_for_each(cur, &my_list) {
        pos = container_of(cur, struct my_data, entry);
        printf("%s\n", pos->name);
    }
    
    return 0;
}

